package com.cly.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cly.base.contants.CacheKeysConstants;
import com.cly.entity.Post;
import com.cly.mapper.PostMapper;
import com.cly.result.JsonRequest;
import com.cly.service.IPostStatisticsService;
import com.cly.statistics.LineStatistics;
import com.cly.statistics.PieStatistics;
import com.cly.statistics.SumStatistics;
import com.cly.vo.PostStatisticsVo;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * 岗位统计服务实现类
 *
 * @author by 春风能解释
 * <p>
 * 2025/2/6
 */
@Service
@RequiredArgsConstructor
public class PostStatisticsServiceImpl extends ServiceImpl<PostMapper, Post> implements IPostStatisticsService {


    /**
     * 岗位30天趋势统计
     *
     * @return JSON
     */
    @Override
    @Cacheable(value = CacheKeysConstants.RecruitKeys.RECRUIT_STATISTICS_LINE)
    public JsonRequest<LineStatistics<String, Long>> statisticsTrend() {
        List<PostStatisticsVo> postStatisticsVos = baseMapper.statisticsTrend();
        List<String> times = new LinkedList<>();
        List<Long> totals = new LinkedList<>();
        postStatisticsVos.forEach(v -> {
            times.add(v.getTime());
            totals.add(v.getTotal());
        });
        return JsonRequest.success(new LineStatistics<String, Long>()
                .setTitle("岗位30天趋势统计")
                .setData(totals)
                .setXAxis(times));
    }

    /**
     * 岗位总数统计
     *
     * @return JSON
     */
    @Override
    @Cacheable(value = CacheKeysConstants.RecruitKeys.RECRUIT_STATISTICS_COUNT)
    public JsonRequest<List<SumStatistics<Long>>> sumStatisticsPost() {
        return JsonRequest.success(baseMapper.sumStatisticsPost());
    }

    /**
     * 岗位地区统计
     *
     * @return JSON
     */
    @Override
    @Cacheable(value = CacheKeysConstants.RecruitKeys.RECRUIT_STATISTICS_PIE)
    public JsonRequest<List<PieStatistics<Long>>> postStatisticsDistrict() {
        return JsonRequest.success(baseMapper.postStatisticsDistrict());
    }


}
